package org.cometmi.gwt.chat.client;

import org.cometmi.gwt.chat.shared.Constants;
import org.cometmi.gwt.chat.shared.SenderObject;
import org.cometmi.gwt.chat.shared.SenderProxy;
import org.cometmi.gwt.client.Connection;
import org.cometmi.gwt.shared.MiQueue;
import org.cometmi.gwt.shared.MiQueuePool;
import org.cometmi.gwt.shared.events.BindCallback;
import org.cometmi.gwt.shared.exceptions.InvalidArgumentException;
import org.cometmi.gwt.shared.exceptions.NameParseException;
import org.cometmi.gwt.shared.exceptions.NotAllowedQueueException;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Text;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;

public class MessengerWidget extends Composite
{
	private final Connection conn = Connection.INSTANCE;

	private ScrollPanel responsesScroll;

	private HTML received;
	private TextArea area;

	private Button send;

	public class SenderObject_ extends SenderObject
	{
		@Override
		public void printMessage(String msg)
		{
			Text text = Document.get().createTextNode(msg);
			Element el = MessengerWidget.this.received.getElement();

			el.appendChild(DOM.createTR());
			el.appendChild(text);

			MessengerWidget.this.responsesScroll.scrollToBottom();
		}

		@Override
		public void printMessage(String msg, String fromUser)
		{
			printMessage(fromUser + ": " + msg);
		}
	}

	public MessengerWidget(final String userName) throws NameParseException, NotAllowedQueueException
	{
		MiQueue qListen = conn.getQueue("org.cometmi.gwt.test.chat." + userName);

		MiQueuePool qSend = conn.getQueuePool("org.cometmi.gwt.test.chat.*");
		final SenderProxy proxy = qSend.proxyToObject(SenderProxy.class, Constants.MESSENGER_ID);

		final Grid grid = new Grid(3, 1);

		qListen.setAttribute("userName", userName, null);
		qListen.bind(Constants.MESSENGER_ID, new SenderObject_(), new BindCallback()
		{
			@Override
			public void onSuccess()
			{
				grid.setWidget(0, 0, responsesScroll);
				grid.setWidget(1, 0, area);
				grid.setWidget(2, 0, send);
			}

			@Override
			public void onError(Throwable t)
			{
				grid.setWidget(0, 0, new HTML("Acess denied because: " + t.getMessage()));
			}
		});

		this.responsesScroll = new ScrollPanel();
		this.responsesScroll.setSize("300px", "300px");
		this.received = new HTML();

		this.responsesScroll.setWidget(this.received);

		this.area = new TextArea();
		this.area.setSize("300px", "50px");

		this.send = new Button("send", new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				try
				{
					proxy.printMessage(MessengerWidget.this.area.getValue(), userName);
				}
				catch (InvalidArgumentException e)
				{}
			}
		});

		initWidget(grid);
	}
}
